package com.dubbo.demo.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dubbo.demo.entity.User;
import com.dubbo.demo.service.TestService;

//http://localhost:8080/dubbo_customer/testSay/aa
//http://localhost:8080/dubbo_customer/testSay/张三
//使用中文注意web.xml配置spring的字符拦截器和tomcat的server.xml为 URIEncoding="utf-8"
//Dubbox的service直接支持rest风格请求及json、xml输出
@Controller
public class TestRestController {

	@Autowired
	private TestService testRestService;//按TestService类型注入，不管是testRestService还是testService，负载均衡
	
	@Autowired
	private TestService testService;//按TestService类型注入，不管是testRestService还是testService，负载均衡
	
	//http://localhost:8080/dubbo_customer/restDubbo
	@RequestMapping("/restDubbo")
	@ResponseBody
	public String restDubbo(HttpServletRequest req){
		User user = testRestService.testSayDubbo();
		return getProgramePath(req)+user.toString();
	}
	//http://localhost:8080/dubbo_customer/restDubbo/张三
	@RequestMapping("/restDubbo/{name}")
	@ResponseBody
	public String restDubboSay(@PathVariable("name") String name,HttpServletRequest req){
		String str = testRestService.say(name);
		return getProgramePath(req)+str;
	}
	
	//http://localhost:8080/dubbo_customer/dubbo
	@RequestMapping("/dubbo")
	@ResponseBody
	public String dubbo(HttpServletRequest req){
		User user = testService.testSayDubbo();
		return getProgramePath(req)+user.toString();
	}
	//http://localhost:8080/dubbo_customer/dubbo/张三
	@RequestMapping("/dubbo/{name}")
	@ResponseBody
	public String dubboSay(@PathVariable("name") String name,HttpServletRequest req){
		String str = testService.say(name);
		return getProgramePath(req)+str;
	}
	
	private String getProgramePath(HttpServletRequest req){
		String url=req.getScheme()+"://"+ req.getServerName()+":"+req.getServerPort()+req.getContextPath();//http://localhost:8080/dubbo_customer
		return url+":";
	}
}
